import { asyncSubmitDealReq, evalEl } from './auth'
import { changeLoadRemoteModel, getBtnOperator } from './hylist'
import { eventOpenGeneralModal } from './hyctr'

import {
  getRemoteUrlDataSetKey,
  getRemoteUrlParam,
  getCommonRemoteUrlVerify, getUrlKey
} from './hyurl'

import {
  getFormModelKeyData
} from './hyform'
import {getBtnDefVal, getOperatorDefVal} from "./hyopen";

export async function beforeOperate(thisObj, globalData = {}, btnItem, operator) {
  const objEvent = 'beforeOperate'
  return await btnOperateEvent(thisObj, globalData, btnItem, operator, objEvent)
}

export async function afterOperate(thisObj, globalData = {}, btnItem, operator) {
  const objEvent = 'afterOperate'
  return await btnOperateEvent(thisObj, globalData, btnItem, operator, objEvent)
}

export async function triggerBtnEvent(thisObj, triggerItem, globalData) {
  const btnEventData = {}
  btnEventData.result = true
  btnEventData.resultTip = '成功执行.'
  const newItem = Object.assign({}, triggerItem)
  if (newItem.fnType === 1 || newItem.fnType === 3 || newItem.fnType === 9 || newItem.fnType === 10 || newItem.fnType === 94 || newItem.fnType === 95 || newItem.fnType === 96) {
    const data = await eventOperateRemoteData(thisObj, globalData, newItem)
    btnEventData.data = data
    return btnEventData
  } else if (newItem.fnType === 2) {
    // 弹框类按钮
    // modal 窗口为异步窗口，没阻塞
    eventOpenGeneralModal(thisObj, newItem, globalData)
  } else {
    console.log('触发按钮类型,暂时不支持.')
  }
  return btnEventData
}

export async function btnOperateEvent(thisObj, globalData = {}, btnItem, operator, objEvent) {
  const operateDataList = []
  const operateResult = {}
  operateResult.result = true
  operateResult.list = operateDataList
  const btnEvent = thisObj.initUiVal.toolbar.btnEvent[btnItem.key]
  if (btnEvent === undefined) {
    return operateResult
  }
  if (btnEvent.length > 0) {
    for (const key in btnEvent) {
      const data = btnEvent[key]
      if (data.s_OBJ_EVENT === objEvent) {
        if (operator === null || operator.s_OBJ_TPL_ID === data.s_OBJ_TPL_ID) {
          const triggerObjList = data.triggerObjList
          // 操作对象
          for (const objKey in triggerObjList) {
            const triggerObj = triggerObjList[objKey]
            if (triggerObj.i_RULE_TPL_PROP === 13) { // 结果数值校验
              const callBtn = thisObj.initUiVal.toolbar.btnData[triggerObj.s_RULE_TPL_KEY]
              // 查询操作
              const tmpData = await asyncCallRemoteData(thisObj, callBtn, globalData)
              const mergeData = Object.assign({}, globalData.globalParam, tmpData)
              const beforeOperateData = evalOperateRuleEl(thisObj, triggerObj, mergeData)
              if (!beforeOperateData.result) {
                operateResult.result = false
                operateResult.resultTip = objEvent + '事件判断表达式不满足执行规则.'
                beforeOperateData.triggerBtn = callBtn
                beforeOperateData.triggerObj = triggerObj
                beforeOperateData.remoteData = tmpData
                operateDataList.push(beforeOperateData)
                return operateResult
              }
            } else if (triggerObj.i_RULE_TPL_PROP === 49 || triggerObj.i_TRIGGER_OBJ === 49) {
              const callBtn = thisObj.initUiVal.toolbar.btnData[triggerObj.s_RULE_TPL_KEY]
              const beforeOperateData = evalOperateRuleEl(thisObj, triggerObj, globalData.globalParam)
              beforeOperateData.triggerBtn = callBtn
              beforeOperateData.triggerObj = triggerObj
              operateDataList.push(beforeOperateData)
            }
          }
        }
      }
    }
  } else {
    operateResult.result = true
    operateResult.resultTip = '无' + objEvent + '事件'
  }
  return operateResult
}

/**
 * 业务子项目规则
 * @param thisObj
 * @param triggerObj 触发对象
 * @param tmpData 数据
 * @returns {{}}
 */
export function evalOperateRuleEl(thisObj, triggerObj, tmpData) {
  const beforeOperateData = {}
  beforeOperateData.result = true
  const objEl = triggerObj.ruleEl
  if (objEl === undefined) {
    return beforeOperateData
  }
  const c = evalEl(objEl, tmpData)
  if (!c) {
    beforeOperateData.result = false
    beforeOperateData.resultTip = triggerObj.s_RULE_TPL_VAL
    beforeOperateData.triggerObj = beforeOperateData
  }
  return beforeOperateData
}

export async function asyncCallRemoteData(thisObj, btnItem, globalData) {
  const operator = getBtnOperator(thisObj, globalData, btnItem)
  const urlKey = getUrlKey(operator, btnItem)
  const param = getRemoteUrlParam(thisObj, urlKey, globalData)
  // const urlDataSetKey = getRemoteUrlDataSetKey(thisObj, urlKey)
  thisObj.loading = true
  return await asyncSubmitDealReq(param).then((res) => {
    thisObj.loading = false
    if (res.data.success) {
      // let tmpKey = 'data'
      // if (urlDataSetKey.S_GET_DATA_KEY !== undefined) {
      //   tmpKey = urlDataSetKey.S_GET_DATA_KEY
      // }
      const tmpData = res.data.data
      return tmpData
    } else {
      thisObj.$message.warning(res.data.message)
    }
  })
}

export async function eventOperateRemoteData(thisObj, globalData = {}, btnItem) {
  const operator = getBtnOperator(thisObj, globalData, btnItem)
  // console.debug('btn operator', operator)
  let operatorDefVal = {}
  if (operator !== undefined && operator !== null) {
    operatorDefVal = getOperatorDefVal(thisObj, operator.refSetOpenVal)
  }
  const urlKey = getUrlKey(operator, btnItem)
  const beforeOperateResult = await beforeOperate(thisObj, globalData, btnItem, operator)
  if (!beforeOperateResult.result) {
    thisObj.$message.warning(beforeOperateResult.resultTip)
    changeLoadRemoteModel(thisObj, beforeOperateResult)
    return
  } else {
    const beforeOperateList = beforeOperateResult.list
    for (const i in beforeOperateList) {
      const operate = beforeOperateList[i]
      const triggerBtn = operate.triggerBtn
      triggerBtnEvent(thisObj, triggerBtn, globalData)
    }
  }
  if (urlKey === 'page') {
    const iPageNo = thisObj.tablePagination.current
    const iPageSize = thisObj.tablePagination.pageSize
    thisObj.tableQueryParam.iPageNo = iPageNo
    thisObj.tableQueryParam.iPageSize = iPageSize
  }
  // const postRecord = Object.assign({}, globalData, thisObj.tableQueryParam)
  let postParam = getRemoteUrlParam(thisObj, urlKey, globalData)
  const defVal = getBtnDefVal(thisObj, btnItem)
  postParam = Object.assign(postParam, defVal, operatorDefVal)
  const verify = getCommonRemoteUrlVerify(thisObj, urlKey, postParam)
  if (verify === 1) {
    // 查询验证未通过
    return
  }
  if (postParam === null || postParam.S_PAGE_ID === '') {
    return
  }
  // const urlDataSetKey = getRemoteUrlDataSetKey(thisObj, urlKey)
  thisObj.loading = true
  const asyncReqData = await asyncSubmitDealReq(postParam).then((res) => {
    thisObj.loading = false
    if (res.data.success) {
      const tmpKey = 'data'
      // if (urlDataSetKey.S_GET_DATA_KEY !== undefined) {
      //   tmpKey = urlDataSetKey.S_GET_DATA_KEY
      // }
      const tmpData = res.data[tmpKey]
      // thisObj.tableData = tmpData
      return tmpData
    } else {
      thisObj.$message.warning(res.data.message)
    }
  })
  const afterOperateResult = await afterOperate(thisObj, globalData, btnItem, operator)
  if (!afterOperateResult.result) {
    thisObj.$message.warning(afterOperateResult.resultTip)
    changeLoadRemoteModel(thisObj, afterOperateResult)
    return
  } else {
    const afterOperateList = afterOperateResult.list
    for (const i in afterOperateList) {
      const operate = afterOperateList[i]
      const triggerBtn = operate.triggerBtn
      triggerBtnEvent(thisObj, triggerBtn, globalData)
    }
  }
  return asyncReqData
}
export async function asyncChangeLoadRemoteModel(thisObj, data) {
  const h = thisObj.$createElement
  return await thisObj.$info({
    title: 'This is a notification message',
    content: h('div', {}, [
      h('p', 'some messages...some messages...'),
      h('p', 'some messages...some messages...')
    ]),
    onClose() { return 1 }
  })
}
